package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.lly.songhigh.R;
import com.lly.songhigh.entity.SingerTop;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/24.
 */
public class SingerTopAdapter extends BaseAdapter {
    private List<SingerTop.DataEntity> dataEntities;
    private Context mContext;
    private RequestQueue queue;
    private VolleySingleton volleySingleton;
    private SingerTop.DataEntity dataEntity;
    private ImageLoader imageLoader;

    public SingerTopAdapter(Context c, List<SingerTop.DataEntity> dataEntities) {
        mContext = c;
        this.dataEntities = dataEntities;
        volleySingleton = VolleySingleton.getInstance();
    }

//    public void initData(List<SingerTop.DataEntity> data) {
//        if (this.data == null) {
//            this.data = new ArrayList<>();
//
//        } else {
//            this.data.clear();
//        }
//        this.data.addAll(data);
//        notifyDataSetChanged();
//    }

    @Override
    public int getCount() {
        return dataEntities != null && dataEntities.size() > 0 ? dataEntities.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 初始化缓存对象
        ViewHolder holder;
        // 布局加载器加载行布局文件
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.fr_singer_listview, null);
            // 初始化缓存组件
            holder = new ViewHolder();

            holder.singer = (TextView) convertView
                    .findViewById(R.id.singer);
            holder.singerImage = (ImageView) convertView
                    .findViewById(R.id.singerImage);
            // 把holder设置到tag里
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /**
         * 设置行布局内容
         */
        dataEntity = (SingerTop.DataEntity) getItem(position);
        if (dataEntity != null) {
            // 设置简介
            holder.singer.setText(dataEntity.getSinger_name());
            // 设置图片
            String url = dataEntity.getPic_url();
            Picasso.with(mContext).load(url).placeholder(R.mipmap.placeholder_avatar_180).error(R.mipmap.placeholder_avatar_180).into(holder.singerImage);
        }
        return convertView;
    }

    /**
     * 组件缓存
     */
    private class ViewHolder {
        public TextView singer;
        public ImageView singerImage;
    }
}

